package dto;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;

import model.Category;
import model.Comment;
import model.Producer;
import model.ProducerOwnCategory;
import model.Product;
import model.User;
import model.YourCart;
import model.YourFavourite;

public class DataManager {
	private String dbURL = "";
	private String dbUserName = "";
	private String dbPassword = "";

	public DataManager() {
		dbURL = "jdbc:mysql://localhost/ecommerce?useUnicode=true&characterEncoding=UTF-8";
		dbUserName = "root";
		dbPassword = "";
		try {
			DriverManager.registerDriver(new com.mysql.jdbc.Driver());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void setDbURL(String dbURL) {
		this.dbURL = dbURL;
	}

	public String getDbURL() {
		return dbURL;
	}

	public void setDbUserName(String dbUserName) {
		this.dbUserName = dbUserName;
	}

	public String getDbUserName() {
		return dbUserName;
	}

	public void setDbPassword(String dbPassword) {
		this.dbPassword = dbPassword;
	}

	public String getDbPassword() {
		return dbPassword;
	}

	// ---------------------------------------------------
	public Connection getConnection() {
		Connection conn = null;
		try {
			conn = DriverManager.getConnection(getDbURL(), getDbUserName(),
					getDbPassword());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}

	// ---------------------------------------------------
	public void closeConnection(Connection conn) {
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	// User
	// ----------------------------------------------------

	public List<User> getAllUser() {
		return DtoUser.getAllUser(this);
	}

	public User getUserWithUserId(int userId) {
		return DtoUser.getUserWithUserID(this, userId);
	}

	public User getUserWithUserName(String userName) {
		return DtoUser.getUserWithUserName(this, userName);

	}

	public boolean insertUser(User user) {
		return DtoUser.insertUser(this, user);
	}

	public boolean deleteUser(int userId) {
		return DtoUser.deleteUser(this, userId);
	}

	public boolean isUser(String username, String password) {
		return DtoUser.isUser(username, password, this);
	}

	public boolean updateMoney(int userId, int money) {
		return DtoUser.updateMoney(this, userId, money);
	}

	public boolean changePassword(int userId, String password) {
		return DtoUser.changePass(this, userId, password);
	}

	// Category
	// ----------------------------------------------------

	public List<Category> getAllCategory() {
		return DtoCategory.getAll(this);
	}

	public Category getCategoryWithCategoryId(int categoryId) {
		return DtoCategory.getById(categoryId, this);
	}

	public void updateCategoryWithCategoryId(Category category) {
		DtoCategory.updateCategoryWithCategoryId(category, this);
	}

	public void deleteCategoryWithCategoryId(int categoryId) {
		DtoCategory.deleteCategory(this, categoryId);
	}

	public void addCategory(Category category) {
		DtoCategory.addCategory(this, category);
	}

	public void getTreeStructure(List<Integer> categoryIds,
			List<String> categoryNames) {
		DtoCategory.getTreeStructure(this, categoryNames, categoryIds, 0);
	}

	public List<Category> getCategorysByParentId(int categoryParentId) {
		return DtoCategory.getByParentId(categoryParentId, this);
	}

	public Category getCategoryByName(String categoryName) {
		return DtoCategory.getByName(categoryName, this);
	}

	// Producer
	// ----------------------------------------------------

	public List<Producer> getAllProducer() {
		return DtoProducer.getAllProducer(this);
	}

	public Producer getProducerWithProducerId(int producerId) {
		return DtoProducer.getProducerWithProducerId(this, producerId);
	}

	public void updateProducerWithProducerId(int producerId, String producerName) {
		DtoProducer
				.updateProducerWithProducerId(producerId, producerName, this);
	}

	public void deleteProducerWithProducerId(int producerId) {
		DtoProducer.deleteCategory(this, producerId);
	}

	public void addProducer(String producerName) {
		DtoProducer.addCategory(this, producerName);
	}

	public List<Producer> getProducerWithCategoryId(int categoryId) {
		return DtoProducer.getProducerWithCategoryId(this, categoryId);
	}

	// Product
	// ----------------------------------------------------

	public Product getProductWithProductId(int productId) {
		return DtoProduct.getProductWithProductId(this, productId);
	}

	public List<Product> getProductByProduerOwnCategory(
			List<ProducerOwnCategory> poc) {
		return DtoProduct.getProductByProducerOwnCategory(poc, this);
	}

	public List<Product> getRecurisveProductWithCategoryId(int categoryId, int n) {
		return DtoProduct.getRecurisveProductWithCategoryId(categoryId, this, n);
	}

	public Product getProductById(int productId) {
		return DtoProduct.getById(productId, this);
	}

	public List<Product> getAllProduct(List<String> categoryNames,
			List<String> producerNames) {
		return DtoProduct.getAllProduct(this, categoryNames, producerNames);
	}

	public void updateProductWithProductId(Product product) {
		DtoProduct.updateProductWithProductId(this, product);
	}

	public void addProduct(Product product) {
		DtoProduct.addProduct(this, product);
	}

	public void deleteProduct(int productId) {
		DtoProduct.deleteProduct(this, productId);
	}

	public List<Product> getLimitProduct(int start, int number) {
		return DtoProduct.getLimitProduct(start, number, this);
	}

	public List<Product> searchProductByName(String productName) {
		return DtoProduct.searchByProductName(productName, this);
	}

	public void getProductByProducerOwnCategoryId(
			int producerOwnCategoryId, List<Integer> productIds,
			List<String> productNames) {
		DtoProduct.getProductWithProducerOwnCategoryId(this,
				producerOwnCategoryId, productIds, productNames);
	}

	// Producer Own Category
	// ----------------------------------------------------

	public ProducerOwnCategory getProducerOwnCategory(int producerOwnCategoryId) {
		return DtoProducerOwnCategory.getProducerOwnCategory(this,
				producerOwnCategoryId);
	}

	public int isHaveProducerOwnCategory(int producerId, int categoryId) {
		return DtoProducerOwnCategory.isHaveProducerOwnCategory(this,
				producerId, categoryId);
	}

	public void addProducerOwnCategory(int producerId, int categoryId) {
		DtoProducerOwnCategory.addProducerOwnCategory(this, producerId,
				categoryId);
	}

	public int getNewProducerOwnCategoryId() {
		return DtoProducerOwnCategory.getNewProducerOwnCategoryId(this);
	}

	// Comment
	// ----------------------------------------------------

	public List<Comment> getAllCommentWithProductId(int productId) {
		return DtoComment.getAllByProductId(this, productId);
	}

	public boolean insertComent(Comment comment) {
		return DtoComment.insert(comment, this);
	}

	// YourCart
	// ----------------------------------------------------------

	public List<YourCart> getAllCartByUserId(int userId) {
		return DtoYourCart.getByUserId(userId, this);
	}

	public boolean insertYourCart(YourCart y) {
		return DtoYourCart.insert(y, this);
	}

	public boolean deleteYourCartItem(int yourCartId) {
		return DtoYourCart.deleteYourCartItem(this, yourCartId);
	}

	public boolean updateYourCart(int yourCartId, int number) {
		return DtoYourCart.updateYourCartItem(this, yourCartId, number);
	}

	public void deleteAllCartByUserId(int userId) {
		DtoYourCart.deleteAllCartByUserId(this, userId);
	}

	public boolean checkCartProduct(int productId, int userId) {
		return DtoYourCart.checkProduct(productId, userId, this);
	}

	// your Favorite
	// --------------------------------------------------------------------

	public List<YourFavourite> getAllFavoriteByUserId(int userId) {
		return DtoYourFavourite.getByUserId(userId, this);
	}

	public boolean insertYourFavorite(YourFavourite y) {
		return DtoYourFavourite.insert(y, this);
	}

	public boolean deleteFavoreteItem(int favoriteId) {
		return DtoYourFavourite.deleteYourFavoriteItem(this, favoriteId);
	}

	public boolean checkFavouriteProduct(int productId, int userId) {
		return DtoYourFavourite.checkProduct(productId, userId, this);
	}

	// Card
	// ------------------------------------------------------------------
	public int getMoneyByCardId(int cardId) {
		return DtoCard.getMoneyByCardId(this, cardId);
	}

	public boolean updateStatusCard(int cardId) {
		return DtoCard.updateStatusCard(this, cardId);
	}

	public int countCardByMoneyAndStatus(int money, int status) {
		return DtoCard.countCardByStatusAndMoney(this, money, status);
	}

	public boolean addCard(int money) {
		return DtoCard.addCard(this, money);
	}
}
